package com.fivetalks.client;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.fivetalks.object.ChatMessage;

public class ChattingActivity extends Activity implements OnClickListener,
		Runnable {
	private ArrayList<ChatMessage> chatList;
	private ChatCustomAdapter mAdapter;
	private ListView list;
	Thread m_thread;
	static final int MAX = 15;
	public ProgressBar pbTimer;

	long roomNumber;
	ImageButton btSend;
	EditText etChat;

	String sendMsg;
	
	Handler mHandler;
	boolean isRun;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chatting);
		chatList = new ArrayList<ChatMessage>();

		list = (ListView) findViewById(R.id.lvList);
		mAdapter = new ChatCustomAdapter(this, R.layout.chatting_row, chatList, "ChattingActivity");
		list.setAdapter(mAdapter);
		
		Intent intent = getIntent();
		roomNumber = (Long) intent.getSerializableExtra("roomNumber");
		
		Thread m_thread_go = new Thread(new Runnable() {
			@Override
			public void run() {
				// 해당 작업을 처리함
				SocketClient sc = SocketClient.getInstance(null, null,
						null, ChattingActivity.this, null, null,null,null);
				try {
					sc.getDos().writeUTF("playChatting");
					sc.getDos().writeUTF(roomNumber + "");
					sendMsg = sc.getNickName() + "님이 입장하셨습니다.";
					sc.getDos().writeUTF(sendMsg);
					Log.d("chatting", "sendMsg : " + sendMsg);
					sc.getDos().flush();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});

		m_thread_go.start();
		
		
		
		

		btSend = (ImageButton) findViewById(R.id.btnSend);
		etChat = (EditText) findViewById(R.id.etChat);
		btSend.setOnClickListener(this);
		new TimerTask().execute(MAX);
		

	}
	
	
	@Override
	protected void onResume() {
		super.onResume();

	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	public void addResult(ChatMessage chat) {
		try{
	//		Thread.sleep(400);
			chatList.add(chat);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void run() {
		SocketClient sc = SocketClient.getInstance(null,null,null,ChattingActivity.this, null,null,null,null);
		try {
			sc.getDos().writeUTF("playChatting");
			sc.getDos().writeUTF(roomNumber + "");
			sendMsg = etChat.getText().toString();
			
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// 해당 작업을 처리함
					etChat.setText("");
				}
			});
			sc.getDos().writeUTF(sendMsg);
			Log.d("chatting", "sendMsg : " + sendMsg);
			sc.getDos().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class TimerTask extends AsyncTask<Integer, Integer, Integer> {

		protected void onPreExecute() {
			pbTimer = (ProgressBar) findViewById(R.id.pbTimer);
			pbTimer.setMax(MAX);
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub

			int num = params[0];
			while (num > 0) {
				try {
					num--;
					publishProgress(num);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return num;
		}

		protected void onProgressUpdate(Integer... params) {
			pbTimer.setProgress(params[0]);
		}

		protected void onPostExecute(Integer result) {
			if (result == 0) {
				Intent intent = new Intent(ChattingActivity.this,
						ArrowFormActivity.class);
				intent.putExtra("roomNumber", roomNumber);
				Log.d("moonki", "도달하였습니다");
				startActivity(intent);
				finish();
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btnSend:

			start();
			break;
		}
	}

}
